﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Hicap.QRCodeWarehouse.Core.Common
{
    public class Encrypt
    {
        public static string SHA256Encrypt(string strIN)
        {
            //string strIN = getstrIN(strIN);
            using (SHA256 sha256 = new SHA256Managed())
            {
                byte[] tmpByte = sha256.ComputeHash(GetKeyByteArray(strIN));

                StringBuilder rst = new StringBuilder();
                foreach (byte t in tmpByte)
                {
                    rst.Append(t.ToString("x2"));
                }

                sha256.Clear();
                return rst.ToString();
            }

            //return GetStringValue(tmpByte);
        }

        private static string GetStringValue(byte[] Byte)
        {
            string tmpString = "";
            UTF8Encoding asc = new UTF8Encoding();
            tmpString = asc.GetString(Byte);
            return tmpString;
        }

        private static byte[] GetKeyByteArray(string strKey)
        {
            UTF8Encoding asc = new UTF8Encoding();
            int tmpStrLen = strKey.Length;
            byte[] tmpByte = asc.GetBytes(strKey);
            return tmpByte;
        }

        public static string HmacSHA256(string AuthorizationServersage, string secret)
        {
            secret = secret ?? "";
            UTF8Encoding encoding = new System.Text.UTF8Encoding();
            byte[] keyByte = encoding.GetBytes(secret);
            byte[] AuthorizationServersageBytes = encoding.GetBytes(AuthorizationServersage);
            using (HMACSHA256 hmacsha256 = new HMACSHA256(keyByte))
            {
                byte[] hashAuthorizationServersage = hmacsha256.ComputeHash(AuthorizationServersageBytes);
                return Convert.ToBase64String(hashAuthorizationServersage);
            }
        }

        private static string Base64(string AuthorizationServersage)
        {
            Encoding encode = Encoding.UTF8;
            byte[] bytedata = encode.GetBytes(AuthorizationServersage);
            string strPath = Convert.ToBase64String(bytedata, 0, bytedata.Length);
            return strPath;
        }
    }
}